package com.lpgu;



import com.lpgu.config.SpringDataJpaConfig;
import com.lpgu.repositories.CustomerRepository;
import com.lpgu.repositories.MyJpaRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.persistence.EntityManager;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;

/**
 * @author: lpgu
 * @describe:MainStart
 * @createAt: 2022/1/3 17:50
 * 打开注解@EnableJpaRepositories(basePackages = "com.lpgu.repositories")
 */
public class MainStart {
    public static void main(String[] args) throws ClassNotFoundException {
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringDataJpaConfig.class);
        //获取entityManager
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = ioc.getBean(LocalContainerEntityManagerFactoryBean.class);
        EntityManager entityManager = entityManagerFactoryBean.createNativeEntityManager(null);

        //获取pojo类，
        // getGenericInterfaces()拿到当前的父接口
        ParameterizedType genericInterfaces =(ParameterizedType) CustomerRepository.class.getGenericInterfaces()[0];
        //拿到接口的泛型<Customer, Long>
        Type actualTypeArgument = genericInterfaces.getActualTypeArguments()[0];
        Class clazz =Class.forName(actualTypeArgument.getTypeName());
        CustomerRepository myJpaRepository =(CustomerRepository) Proxy.newProxyInstance(CustomerRepository.class.getClassLoader(),
                new Class[]{CustomerRepository.class}, new MyJpaRepository(entityManager,clazz));
        myJpaRepository.findById(3L);
    }
}
